package com.wave.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wave.common.annotation.Log;
import com.wave.common.annotation.RepeatSubmit;
import com.wave.common.core.controller.BaseController;
import com.wave.common.core.domain.PageQuery;
import com.wave.common.core.domain.R;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.validate.AddGroup;
import com.wave.common.core.validate.EditGroup;
import com.wave.common.enums.BusinessType;
import com.wave.common.utils.poi.ExcelUtil;
import com.wave.system.domain.bo.SysProductConfigBo;
import com.wave.system.domain.vo.SysProductConfigVo;
import com.wave.system.service.ISysProductConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 产品配置
 *
 * @author wave
 * @date 2024-10-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/productConfig")
public class SysProductConfigController extends BaseController {

    private final ISysProductConfigService iSysProductConfigService;

    /**
     * 查询产品配置列表
     */
    @SaCheckPermission("system:productConfig:list")
    @GetMapping("/list")
    public TableDataInfo<SysProductConfigVo> list(SysProductConfigBo bo, PageQuery pageQuery) {
        return iSysProductConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品配置列表
     */
    @SaCheckPermission("system:productConfig:export")
    @Log(title = "产品配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysProductConfigBo bo, HttpServletResponse response) {
        List<SysProductConfigVo> list = iSysProductConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品配置", SysProductConfigVo.class, response);
    }

    /**
     * 获取产品配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:productConfig:query")
    @GetMapping("/{id}")
    public R<SysProductConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(iSysProductConfigService.queryById(id));
    }

    /**
     * 新增产品配置
     */
    @SaCheckPermission("system:productConfig:add")
    @Log(title = "产品配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysProductConfigBo bo) {
        if (!iSysProductConfigService.checkProductNameUnique(bo)) {
            return R.fail("新增产品'" + bo.getProductName() + "'失败，产品名称已存在");
        } else if (!iSysProductConfigService.checkProductNumberUnique(bo)) {
            return R.fail("新增产品'" + bo.getProductName() + "'失败，产品型号已存在");
        }
        return toAjax(iSysProductConfigService.insertByBo(bo));
    }

    /**
     * 修改产品配置
     */
    @SaCheckPermission("system:productConfig:edit")
    @Log(title = "产品配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysProductConfigBo bo) {
        if (!iSysProductConfigService.checkProductNameUnique(bo)) {
            return R.fail("修改产品'" + bo.getProductName() + "'失败，产品名称已存在");
        } else if (!iSysProductConfigService.checkProductNumberUnique(bo)) {
            return R.fail("修改产品'" + bo.getProductName() + "'失败，产品型号已存在");
        }
        return toAjax(iSysProductConfigService.updateByBo(bo));
    }

    /**
     * 删除产品配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:productConfig:remove")
    @Log(title = "产品配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        if (iSysProductConfigService.checkProductHasBeUsed(ids)){
            return R.fail("该型号已被生产排布使用，不能删除!");
        }
        return toAjax(iSysProductConfigService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询所有产品配置
     *
     * @param bo
     * @return
     */
    @GetMapping("/listAll")
    public R<List<SysProductConfigVo>> listAll(SysProductConfigBo bo) {
        return R.ok(iSysProductConfigService.queryList(bo));
    }
}
